package pl.eurobank.dev.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import pl.eurobank.dev.hibernate.utils.HibernateUtil;
import pl.eurobank.dev.model.Client;

public class ClientDAOImpl {

	public Client getClientByName() {

		// get actual client from Spring Security Object
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    Session session = HibernateUtil.getSession();	   

	    // associate actual user with client in database
	    String queryString = "FROM Client where username = :username";  
	    Query query2 = session.createQuery(queryString);  
	    query2.setString("username", name);  
	    Object queryResult = query2.uniqueResult(); 
	    Client client = (Client)queryResult;
		
		return client;
		
	}

}
